CMAKE_MINIMUM_REQUIRED(VERSION 2.6 FATAL_ERROR)
CMAKE_POLICY(VERSION 2.6)

SET(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" "${CMAKE_MODULE_PATH}")

FIND_PACKAGE(Torch REQUIRED)
FIND_PACKAGE(Protobuf REQUIRED)

INCLUDE_DIRECTORIES("${Torch_INSTALL_INCLUDE}/TH" ${PROTOBUF_INCLUDE_DIRS})

LINK_DIRECTORIES("${Torch_INSTALL_LIB}")


PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS caffe.proto)

SET(src loadcaffe.cpp ${PROTO_SRCS})

FILE(GLOB luasrc *.lua)

IF(MSVC)
ADD_DEFINITIONS(/std:c++latest)
ADD_DEFINITIONS(/D_CRT_SECURE_NO_WARNINGS=1)
SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
ELSE()
add_definitions(-std=c++11)
ENDIF()

ADD_LIBRARY(loadcaffe MODULE ${src})
TARGET_LINK_LIBRARIES(loadcaffe TH ${PROTOBUF_LIBRARIES})

### Torch packages supposes libraries prefix is "lib"
SET_TARGET_PROPERTIES(loadcaffe PROPERTIES
  PREFIX "lib"
  IMPORT_PREFIX "lib")
    
INSTALL(TARGETS loadcaffe
  RUNTIME DESTINATION "${Torch_INSTALL_LUA_CPATH_SUBDIR}"
  LIBRARY DESTINATION "${Torch_INSTALL_LUA_CPATH_SUBDIR}")

INSTALL(
  FILES
  ${luasrc}
  DESTINATION "${Torch_INSTALL_LUA_PATH_SUBDIR}/loadcaffe")
